<!--
//
// Trigger.vue
// vue3-element-admin
//
// Created by 区区电脑 on 2023/04/11.
//
-->
<template>
    <div class="Index">
        <!-- 筛选区 -->
        <table-search-bar :config="requestBodyConfig" :model="requestBody" title="字典类型" @search="searchClicked" />
        <!-- 内容 -->
        <div class="container">
            <!-- 功能按钮 -->
            <div class="table-buttons">
                <el-button v-permission="permission.add" type="primary" @click="addData">
                    <b-icon name="bi-plus" size="20" />
                    添加
                </el-button>
            </div>
            <!--checkbox设置 :checkbox-config="{trigger: 'row', highlight: true}" -->
            <!--radio设置 :radio-config="{trigger: 'row', highlight: true}" -->
            <vxe-table ref="VXETable" :column-config="{ resizable: false}" :data="state.list" :height="tableHeight" :loading="state.isLoading" :radio-config="{trigger: 'row', highlight: true}" :row-config="{isCurrent: true, isHover: true, keyField: 'id'}" :tree-config="{childrenField: 'children', reserve: true}" border round show-overflow size="small" @current-change="() => {}" @radio-change="dictTypeSelected">
                <template #empty>
                    <el-empty :image-size="100" />
                </template>
                <vxe-column align="center" type="radio" width="50"></vxe-column>
                <vxe-column align="left" field="name" title="名称" width="140"></vxe-column>
                <vxe-column field="code" title="编号" width="140"></vxe-column>
                <vxe-column field="sort" title="排序" width="60"></vxe-column>
                <vxe-column field="remark" min-width="120" title="备注"></vxe-column>
                <vxe-column :resizable="false" field="action" fixed="right" title="操作" :width="$widthColumn(100)">
                    <template #default="{row}">
                        <table-action :actions="[
                            {title: '修改', permission: permission.edit, type: 'primary', ok: editData},
                            {title: '删除', permission: permission.delete, type: 'danger', ok: deleteData, confirm: '您确定要删除当前数据么?'},
                        ]" :data="row" />
                    </template>
                </vxe-column>
            </vxe-table>
            <div class="page-config">
                <vxe-pager v-model:current-page="requestParams.pageIndex" v-model:page-size="requestParams.pageSize" :layouts="['PrevJump', 'PrevPage', 'JumpNumber', 'NextPage', 'NextJump', 'Sizes', 'FullJump', 'Total']" :page-sizes="pageConfig.sizes" :total="pageConfig.total" background size="mini" @page-change="onPageChange" />
            </div>
        </div>
    </div>
    <EditForm ref="editForm" />
</template>

<script setup>
import {ref, reactive, computed, onMounted, inject} from "vue";
import {useStore} from "vuex";
import {useRouter} from "vue-router";
import EditForm from "./EditForm.vue";
import * as api from "./api.js"

// 框架容器区域高度
const containerHeight = computed(() => store.getters.containerHeight)

const {$message, $widthColumn} = window;

const props = defineProps({});
const emits = defineEmits(["typeChange"]);

// 路由
const router = useRouter();
// store
const store = useStore();


// 当前页面权限
const permission = {
    add: "SysDict:AddDictType",
    edit: "SysDict:UpdateDictType",
    delete: "SysDict:DeleteDictType",
    valueList: "SysDict:GetDictDataPageList"
};



// 响应式数据
const state = reactive({
    // 表格数据加载中
    isLoading: false,
    // 表格数据
    list: []
})

const tableHeight = computed(() => store.getters.layoutType === "mobile" ? "" : containerHeight.value - 240)

// 请求query参数
const requestParams = reactive({
    orderCol: "sort",
    isDesc: false,
    pageIndex: 1,
    pageSize: 20,
});
// 请求body参数
const requestBody = reactive({
    name: null,
    code: null,
})
// 筛选区域配置
const requestBodyConfig = reactive({
    name: {label: "名称", component: "el-input"},
    code: {label: "编号", component: "el-input", inMore: true},
})
// 分页配置
const pageConfig = reactive({
    total: 0,
    sizes: [10, 20, 50, 100, 200]
})

// 表单
const editForm = ref();

// 添加
function addData() {
    // console.log(editForm)
    editForm.value.show({
        title: "添加",
        ok: () => {
            reload();
        }
    })
}

// 删除
async function deleteData({id}) {
    console.log("删除: ", id)
    const {success, data} = await api.DeleteDictType({id});
    if (!success) return 0;
    reload();
    $message.success("删除成功")
}

// 修改
function editData(row) {
    editForm.value.show({
        title: "修改",
        options: {...row},
        ok: () => {
            reload();
        }
    })
}

// 点击搜索
function searchClicked() {
    console.primary("搜索条件: ", requestBody)
    reload(true);
}

// vxe-table
const VXETable = ref();

// 查询列表
async function loadList() {
    const params = {
        ...requestParams
    };
    const body = {
        ...requestBody
    };
    state.isLoading = true;
    const {success, data} = await api.GetDictTypePageList(params, body);
    state.isLoading = false;
    if (!success) return 0;
    const {items, totalCount} = data;
    state.list = items;
    pageConfig.total = totalCount;
    if (items.length > 0) {
        setTimeout(() => {
            VXETable.value.setRadioRow(items[0])
            VXETable.value.setCurrentRow(items[0])
            dictTypeSelected({row: items[0]})
        }, 300)
    }
}

// 分页事件
function onPageChange({type}) {
    if (type === "current") {
        loadList()
    }
    if (type === "size") {
        reload(true)
    }
}

// 选中字典类型
function dictTypeSelected({row}) {
    // console.log(e)
    emits("typeChange", row)
}

// 刷新
function reload(resetPageIndex) {
    if (resetPageIndex) requestParams.pageIndex = 1;
    loadList()
}

onMounted(() => {
    loadList()
})
</script>

<style lang="scss" scoped>
.Index {
    margin: 10px;
    width: calc(100% - 20px);
    box-sizing: border-box;

    .container {
        padding: 10px;
        background: #fff;
        margin-top: 10px;
        box-shadow: 0 0 10px -2px rgba(0, 0, 0, 0.1);
    }
}
</style>
